<script setup lang="ts">
import { ref, onMounted } from 'vue'
import {
  getSkuListAPI,
  cancelSaleAPI,
  onSaleAPI,
  getSkuInfoAPI,
  deleteSkuAPI,
} from '@api/product/sku/index.ts'
import type {
  skuData,
  skuResponseDataType,
  skuInfoData,
} from '@api/product/sku/type.ts'
import { ElMessage } from 'element-plus'

const currentPage = ref<number>(1)
const pageSize = ref<number>(10)
const total = ref<number>(0)
const loading = ref<boolean>(false)

// 分页器事件
const handleSizeChange = (e: number) => {
  pageSize.value = e
  getSkuListFn()
}
const handleCurrentChange = (e: number) => {
  currentPage.value = e
  getSkuListFn()
}

// 获取数据
const skuList = ref<skuData[]>([])
const getSkuListFn = async () => {
  loading.value = true
  const res: skuResponseDataType = await getSkuListAPI(
    currentPage.value,
    pageSize.value,
  )
  skuList.value = res.data.records
  total.value = res.data.total
  loading.value = false
}
onMounted(() => getSkuListFn())

// 点击上架/下架按钮
const handleSaleChangeFn = (row: skuData) => {
  if (row.isSale) {
    cancelSaleAPI(row.id as number).then(() => {
      getSkuListFn()
    })
  } else {
    onSaleAPI(row.id as number).then(() => {
      getSkuListFn()
    })
  }
}

// 抽屉组件显隐
const showDrawer = ref<boolean>(false)

// 展示详情
const skuInfo = ref<skuData>({})
const showInfoFn = (row: skuData) => {
  showDrawer.value = true
  getSkuInfoAPI(row.id as number).then((res: skuInfoData) => {
    skuInfo.value = res.data
  })
}

const handleDeleteFn = (row: skuData) => {
  deleteSkuAPI(row.id as number).then(res => {
    if(res.code === 200) {
      getSkuListFn()
    } else {
      ElMessage.error(res.data)
    }
  }).catch(err => {
    ElMessage.error(err.message)
  })
}
</script>

<template>
  <el-card>
    <el-table v-loading="loading" :data="skuList" border style="margin: 20px 0">
      <el-table-column
        align="center"
        label="序号"
        type="index"
        width="100px"
      ></el-table-column>
      <el-table-column
        prop="skuName"
        label="名称"
        show-overflow-tooltip
        width="200px"
      ></el-table-column>
      <el-table-column
        prop="skuDesc"
        label="描述"
        show-overflow-tooltip
        width="200px"
      ></el-table-column>
      <el-table-column label="默认图片" width="150px">
        <template #default="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="重量（g）"
        width="150px"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="价格（元）"
        width="150px"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="250px">
        <template #default="{ row }">
          <el-button
            :icon="row.isSale ? 'Top' : 'Bottom'"
            type="info"
            @click="handleSaleChangeFn(row)"
          ></el-button>
          <el-button icon="Edit" type="primary"></el-button>
          <el-button
            icon="InfoFilled"
            type="primary"
            @click="showInfoFn(row)"
          ></el-button>
          <el-popconfirm
            title="是否要删除该数据？"
            @confirm="handleDeleteFn(row)"
          >
            <template #reference>
              <el-button icon="Delete" type="danger"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 40, 100]"
      :small="true"
      :background="true"
      layout="prev, pager, next, jumper, ->, total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 抽屉组件展示商品详情 -->
    <el-drawer v-model="showDrawer" direction="rtl">
      <template #header>查看商品详情</template>
      <template #default>
        <el-row>
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag v-for="item in skuInfo.skuAttrValueList" :key="item.attrId">
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.saleAttrId"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img :src="item.imgUrl" alt="" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<style scoped>
.el-row {
  margin: 20px 0;
}
.el-tag {
  margin: 0 15px 15px;
}

.el-carousel__item img {
  width: 200px;
  height: 120px;
}
</style>
